package com.hys.rabbitmq.send;

import java.util.Date;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.hys.rabbitmq.config.RabbitConfig;

@Component
public class Sender {
	
	@Autowired
	private AmqpTemplate rabbitTemplate;

	/**
	 * 单个
	 */
	public void send() {
		String context = "hello " + new Date();
		System.out.println("Sender 1111: " + context);
		//this.rabbitTemplate.convertAndSend("hello", context);
		this.rabbitTemplate.convertAndSend(RabbitConfig.FanoutExchange,"hello",context);
		//rabbitTemplate.
	}
	/**
	 * 多个
	 */
	public void sends() {
		for(int i = 0 ;i<20;i++){
			String context = "hellos "+(i+1) + "  "+new Date();
			System.out.println("Sender : " + context);
			this.rabbitTemplate.convertAndSend("hellos", context);	
		}
		//this.rabbitTemplate.convertAndSend(context);
	}
	
	/**
	 *  在direct类型的exchange中，只有这两个routingkey完全相同
	 *  ，exchange才会选择对应的binging进行消息路由。
	 */
	public void directSend(){
		for(int i = 0 ;i<20;i++){
			String context = "directSend "+(i+1) + "  "+new Date();
			System.out.println("directSend : " + context);
			this.rabbitTemplate.convertAndSend(RabbitConfig.DirectExchange,RabbitConfig.DirectExchangeKey1, context);	
		}
	}
	
	public void topic1(){
		for(int i = 0 ;i<20;i++){
			String context = "topic1 "+(i+1) + "  "+new Date();
			System.out.println("topic1 : " + context);
			this.rabbitTemplate.convertAndSend(RabbitConfig.TopicExchange,RabbitConfig.TopicName, context);	
		}
	}
	
	public void topicall(){
		for(int i = 0 ;i<20;i++){
			String context = "topicall "+(i+1) + "  "+new Date();
			System.out.println("topicall : " + context);
			this.rabbitTemplate.convertAndSend(RabbitConfig.TopicExchange,RabbitConfig.TopicNames, context);	
		}
	}
	
}